home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / GENERICP.ROC < prev    next >
Text File  |  1992-12-02  |  8KB  |  277 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Generic Procs for building new commands
  5.  *
  6.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  7.  *
  8.  *
  9.  *    RESTRICTIONS ON MacShell program and source code.
  10.  *
  11.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  12.  *    restricted use by the owner of the CDROM "Disk to the future II".
  13.  *
  14.  *    Ñ╩No permission is granted for any commercial use without the written
  15.  *    consent of the Suick Bay Technologies.
  16.  *
  17.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  18.  *    the written consent of the Suick Bay Technologies.
  19.  *
  20.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  21.  *
  22.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  23.  *    distribute it with or within a commercial product without the written
  24.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  25.  *    the author for inclusion in updates to the program.  Thanks.
  26.  *
  27.  *
  28.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  29.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  30.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  31.  *
  32.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  33.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  34.  *    OR ANY PART THEREOF. 
  35.  *
  36.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  37.  *    or profits or other special, indirect and consequential damages, even if
  38.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  39.  *
  40.  *    Suick Bay Technologies can be reached at:
  41.  *    
  42.  *    8768 Cottonwood lane
  43.  *    Maple Grove, MN 55369
  44.  *    Voice: (612) 425-7025
  45.  *    AppleLink: D5233
  46.  *    
  47.  *
  48.  *    No parts of this software may be reproduced or stored in a
  49.  *    retrieval system or transmitted in any form, or any means,
  50.  *    electronic, mechanical, photocopying, recording or otherwise,
  51.  *    without the prior written permission of Suick Bay Technologies.
  52.  *    
  53.  *    Spread the word and not the disk.
  54.  *    
  55.  *    This file contains 'Generic' process functions that can be used
  56.  *    create new commands.  Two formats are provided.  The first 
  57.  *    format is used for simple commands that do not require path
  58.  *    searching.  Simple commands that were created from this format
  59.  *    were echo, sleep, etc.  The second format is used for commands that
  60.  *    process files.  These commands rquire the use of the ExpandPath
  61.  *    function and a call-back function.  ExpandPath is passed a 
  62.  *    path that is used to search for a file, directory or volume.
  63.  *    When a match is found, the ExpandPath function then calls the
  64.  *    provided callback function with the appropriate information.
  65.  *    This feature allows the shell to expand  'wildcard' patterns
  66.  *    that are specified in path names.
  67.  *
  68.  *    The functions are listed with XXX and YYY in their names.  in
  69.  *    general the XXX should be substituted with the function name.
  70.  *    For example wc (for word count).  THe YYY should be substituted
  71.  *    with the type of date that the function operates on.  For
  72.  *    example File.
  73.  *    
  74.  *    For examples of procs that use stdin refer to the command
  75.  *    source code product.
  76.  *    
  77.  */
  78.  
  79. #include    "SystemPub.h"
  80. #include    "Proc.h"
  81. #include    "ShellPub.h"
  82. #include    "Prefs.h"
  83.  
  84. /*******************************************************************
  85.  *
  86.  *    Function XXX
  87.  *
  88.  *    usage XXX [options] [names]    
  89.  *
  90.  *
  91.  *******************************************************************/
  92.  
  93. void        XXXYYY( WHandle ShellWh, int16 PID, char *argument )
  94. {
  95. /*
  96.  *    Process the argument here
  97.  */
  98.     procPrintf( ShellWh, PID, "XXXYYY arg %s\n", argument );
  99. }
  100.  
  101. /*******************************************************************/
  102.  
  103. Boolean            DoXXX( int16 signal, WHandle ShellWh, int16 PID,
  104.                     char *string )
  105. {
  106. int16            i, argc;
  107. char            *cp, argument[ 256 ];
  108. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  109.  
  110.     switch( signal )
  111.         {
  112.         case    PROC_INIT    :
  113.             (**MyShell).Proc[ PID ].flags = TRUE;        /* flag the init */
  114.             break;
  115.             
  116.         case    PROC_TERM    :                            /* user break or quit ! */
  117.         case    PROC_BREAK    :
  118.             /* Tell the shell that we're done */
  119.             SendOutToken( ShellWh, PID, PROC_BREAK );
  120.             /* Turn ourself off */
  121.             (**MyShell).Proc[ PID ].ProcActive = FALSE;
  122.             break;
  123.             
  124.         case    PROC_STDIN    :
  125.             if( (**MyShell).Proc[ PID ].flags )            /* were we inited ? */
  126.                 {
  127.                 (**MyShell).Proc[ PID ].flags = FALSE;    /* we only want to execute once */
  128.  
  129.                 /* get arguments */
  130.                 argc = (**MyShell).Proc[ PID ].argc;        /* number of arguments */
  131.                 
  132.                 for( i = 1; i < argc; i++ )                    /* process options first */
  133.                     {
  134.                     GetArgv( ShellWh, PID, i, argument );
  135.                     cp = argument;
  136.         
  137.                     if( *cp++ == '-' )
  138.                         while( *cp )            /* scan characters after - */
  139.                             switch( *cp++ )
  140.                                 {
  141.                                 case    'x'    :    /* option x */
  142.                                     /* set option flags here */
  143.                                     break;
  144.                                 }
  145.                     }
  146.  
  147.                 for( i = 1; i < argc; i++ )                    /* process arguments next */
  148.                     {
  149.                     GetArgv( ShellWh, PID, i, argument );
  150.                     if( *argument != '-' )                    /* don't process options */
  151.                         {
  152.                         XXXYYY( ShellWh, PID, argument );
  153.                         ResetShellPWD( ShellWh );
  154.                         }
  155.                     }
  156.  
  157.                 /* Tell the shell that we're done */
  158.                 SendOutToken( ShellWh, PID, PROC_BREAK );
  159.                 /* Turn ourself off */
  160.                 (**MyShell).Proc[ PID ].ProcActive = FALSE;
  161.                 
  162.                 return( TRUE );    /* return status */
  163.                 }
  164.         }
  165. }
  166.  
  167. /*******************************************************************
  168.  *
  169.  *    Function XXX
  170.  *
  171.  *    PathName Callback function
  172.  *
  173.  *    usage XXX [options] [names]    
  174.  *
  175.  *
  176.  *******************************************************************/
  177.  
  178. void        XXXCallBack( WHandle ShellWh, int16 PID,
  179.                 char *path,        /* full path to this object */
  180.                 char *last,        /* name of this object */
  181.                 pathType what,    /* what the object is */
  182.                 int16 vRefNum,    /* objects volume reference number */
  183.                 int32 dirID )    /* objects directory ID */
  184. {
  185.     switch( what )
  186.         {
  187.         case    pathIsFile        :    /* process file */
  188.             procPrintf( ShellWh, PID, "XXXYYY found FILE %s\n", last );
  189.             break;
  190.             
  191.         case    pathIsDir        :    /* process directory */
  192.             procPrintf( ShellWh, PID, "XXXYYY found DIRECTOY %s\n", path );
  193.             break;
  194.             
  195.         case    pathIsVol        :    /* process volume */
  196.             procPrintf( ShellWh, PID, "XXXYYY found VOLUME %s\n", last );
  197.             break;
  198.         }
  199. }
  200.  
  201. void        XXXYYY( WHandle ShellWh, int16 PID, char *argument )
  202. {
  203. ShellWindRec    **MyShell;
  204.  
  205.     /* get the shells date object */
  206.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  207.  
  208.     /* expand pathname */
  209.     ExpandPath( ShellWh, PID, argument, XXXCallBack,
  210.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  211.  
  212.     /* reset the shell to its previous working directory */
  213.  
  214.     ResetShellPWD( ShellWh );
  215. }
  216.  
  217. /*******************************************************************/
  218.  
  219. Boolean            DoXXX( int16 signal, WHandle ShellWh,
  220.                     int16 PID, char *string )
  221. {
  222. int16            i, argc;
  223. char            *cp,
  224.                 argument[ 256 ];
  225. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  226.  
  227.     switch( signal )
  228.         {
  229.         case    PROC_INIT    :
  230.             (**MyShell).Proc[ PID ].flags = TRUE;        /* flag the init */
  231.             break;
  232.             
  233.         case    PROC_TERM    :
  234.         case    PROC_BREAK    :
  235.             /* Tell the shell that we're done */
  236.             SendOutToken( ShellWh, PID, PROC_BREAK );
  237.             /* Turn ourself off */
  238.             (**MyShell).Proc[ PID ].ProcActive = FALSE;
  239.             break;
  240.             
  241.         case    PROC_STDIN    :
  242.             if( (**MyShell).Proc[ PID ].flags )
  243.                 {
  244.                 (**MyShell).Proc[ PID ].flags = FALSE;        /* we only want to execute once */    
  245.  
  246.                 /* get arguments */
  247.                 argc = (**MyShell).Proc[ PID ].argc;
  248.                 for( i = 1; i < argc; i++ )
  249.                     {
  250.                     GetArgv( ShellWh, PID, i, argument );
  251.                     cp = argument;
  252.         
  253.                     if( *cp++ == '-' )
  254.                         while( *cp )
  255.                             switch( *cp++ )
  256.                                 {
  257.                                 case    'x'    :    /* option x */
  258.                                     break;
  259.                                 }
  260.                     }
  261.  
  262.                 for( i = 1; i < argc; i++ )
  263.                     {
  264.                     GetArgv( ShellWh, PID, i, argument );
  265.                     if( *argument != '-' )                    /* don't process options */
  266.                         XXXYYY( ShellWh, PID, argument );
  267.                     }
  268.                     
  269.                 /* Tell the shell that we're done */
  270.                 SendOutToken( ShellWh, PID, PROC_BREAK );
  271.                 /* Turn ourself off */
  272.                 (**MyShell).Proc[ PID ].ProcActive = FALSE;
  273.                 return( FALSE );
  274.                 }
  275.         }
  276. }
  277.